package com.boot.message.netty.server.handler.impl;

import com.boot.message.netty.server.handler.AbstractServerChannelInitializer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.charset.StandardCharsets;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @date 2019/11/21 13:42
 **/

public class JSONServerInitializer extends AbstractServerChannelInitializer<SocketChannel> {

    JSONServerHandler jsonServerHandler;

    public JSONServerInitializer(JSONServerHandler jsonServerHandler) {
        this.jsonServerHandler = jsonServerHandler;
    }

    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();
        ByteBuf delimiter = Unpooled.copiedBuffer("|".getBytes());
        pipeline.addLast(new DelimiterBasedFrameDecoder(9096, delimiter));
        pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8));
        pipeline.addLast(new StringEncoder(StandardCharsets.UTF_8));
        pipeline.addLast(jsonServerHandler);
    }
}
